import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:hzy_normal_tool/hzy_normal_tool.dart';

import '../../tools/index.dart';

import '../widgets/widgets.dart';

/// 配置结束刷新
PageState configEndRefresh({
  int? type,
  PageState? state,
  EasyRefreshController? refreshController,
}) {
  IndicatorResult result = IndicatorResult.success;

  if (state == PageState.errorState ||
      state == PageState.erroronlyTotal ||
      state == PageState.errorshowRelesh) {
    result = IndicatorResult.fail;
  } else if (state == PageState.noMoreDataState && type == 2) {
    result = IndicatorResult.noMore;
    state = PageState.dataFetchState;
  }
  if (refreshController != null) {
    if (type != 2 && refreshController.controlFinishRefresh) {
      refreshController.finishRefresh(result);
      if (state == PageState.noMoreDataState &&
          refreshController.controlFinishLoad) {
        refreshController.finishLoad(IndicatorResult.noMore);
      }
    } else if (type == 2 && refreshController.controlFinishLoad) {
      refreshController.finishLoad(result);
    }
  }
  return state ?? PageState.dataFetchState;
}

// 展示自定义加载动画
void showLoad({
  String? msg,
}) {
  EasyLoading.instance.backgroundColor = Colors.white.withOpacity(0);
  EasyLoading.instance.maskColor = Colors.white.withOpacity(0);
  EasyLoading.instance.indicatorColor = Colors.white.withOpacity(0);
  EasyLoading.instance.textColor = Colors.white.withOpacity(1);
  EasyLoading.instance.loadingStyle = EasyLoadingStyle.custom;
  EasyLoading.instance.radius = 12;
  EasyLoading.instance.contentPadding = EdgeInsets.symmetric(
    vertical: 15.0,
    horizontal: msg == null ? 20 : 30.0,
  );
  EasyLoading.instance.textPadding = EdgeInsets.zero;
  EasyLoading.instance.boxShadow = [
    BoxShadow(
      color: HzyNormalColorS.col000000.withOpacity(0),
    )
  ];
  EasyLoading.show(
    indicator: configLoadWidget(),
    maskType: EasyLoadingMaskType.custom,
    dismissOnTap: true,
    status: msg,
  );
}

/// 显示toast msg
/// 传content 后,如果没传ontap 会调用返回上一个界面的方法
/// type == 1 普通 toast , 2 成功toast 3 失败toast
void showToastMsg({
  required String msg,
  int type = 1,
  EasyLoadingStyle loadingStyle = EasyLoadingStyle.dark,
  BuildContext? context,
  Function()? onTap,
}) {
  EasyLoading.instance.loadingStyle = loadingStyle;
  if (type == 1) {
    EasyLoading.showToast(msg);
  } else if (type == 2) {
    EasyLoading.showSuccess(msg);
  } else if (type == 3) {
    EasyLoading.showError(msg);
  }
  if (onTap != null || context != null) {
    Future.delayed(EasyLoading.instance.displayDuration).then((value) {
      if (onTap != null) {
        onTap();
      } else if (context != null && context.mounted) {
        Navigator.of(context).pop();
      }
    });
  }
}

// 显示默认加载动画
void showNormalLoadMsg({
  String? msg,
  EasyLoadingStyle loadingStyle = EasyLoadingStyle.dark,
}) {
  EasyLoading.instance.loadingStyle = loadingStyle;
  EasyLoading.show(status: msg);
}

// 取消加载动画
void loadDismiss() {
  EasyLoading.dismiss();
}

/// 配置通用底部安全区域
configSafeBottomHeight({
  double normalHeight = 20,
}) {
  return ScreeTool.bottomBarHeight == 0
      ? normalHeight
      : ScreeTool.bottomBarHeight;
}
